﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace QuanLySuKien
{
    public partial class Them_Sua_Xoa_NhanSu : System.Web.UI.Page
    {
        private DataTable dt = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();
        Connect conn = new Connect();
        protected void Page_Load(object sender, EventArgs e)
        {
            conn.connect();
            getdata();
        }
        public void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = "select * from NhanSu";
            da.SelectCommand = command;
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            LabelStatus.Text = "";
            if (TextBox1.Text == "" | TextBox2.Text == "" | TextBox3.Text == "" | TextBox4.Text == "" | DropDownList2.Text == "")
                LabelStatus.Text = "bạn chưa nhập đủ thông tin";
            else
            {
                if (check("Select MaNS from NhanSu where MaNS = @DoiSo", TextBox1.Text) == true)
                    LabelMaNS.Text = "Mã nhân sự đã tồn tại";
                else
                {
                    DataRow row = dt.NewRow();
                    row["MaNS"] = TextBox1.Text;
                    row["LoaiNhanSu"] = TextBox4.Text;
                    row["Gia"] = TextBox2.Text;
                    row["Mota"] = TextBox3.Text;
                    row["TrangThai"] = DropDownList2.Text;
                    dt.Rows.Add(row);
                    SqlCommand commandInsert = new SqlCommand();
                    commandInsert.Connection = conn.con;
                    commandInsert.CommandType = CommandType.Text;
                    commandInsert.CommandText = @"Insert NhanSu (MaNS, LoaiNhanSu, Gia, Mota, TrangThai) Values 
                                                ( @MaNS, @LoaiNhanSu,  @Gia, @Mota, @TrangThai)";
                    commandInsert.Parameters.Add("@MaNS", SqlDbType.VarChar, 10, "MaNS");
                    commandInsert.Parameters.Add("@LoaiNhanSu", SqlDbType.NVarChar, 50, "LoaiNhanSu");
                    commandInsert.Parameters.Add("@Gia", SqlDbType.Money, 50, "Gia");
                    commandInsert.Parameters.Add("@Mota", SqlDbType.NVarChar, 50, "Mota");
                    commandInsert.Parameters.Add("@TrangThai", SqlDbType.Int, 50, "TrangThai");
                    da.InsertCommand = commandInsert;
                    da.Update(dt);
                    dt.Clear();
                    getdata();
                    LabelStatus.Text = "bạn đã thêm thành công";
                    LabelMaNS.Text = "";
                    TextBox1.Enabled = false;
                }
            }
        }
        private bool check(String s, String s1)
        {
            bool check1 = false;
            DataTable data = new DataTable();
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = s;
            command.Parameters.Add("@DoiSo", SqlDbType.VarChar, 10).Value = s1;
            da.SelectCommand = command;
            da.Fill(data);
            if (data.Rows.Count > 0) check1 = true;
            return check1;
        }


        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int dong = GridView1.SelectedIndex;
            this.TextBox1.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[1].Text.ToString()));
            this.TextBox2.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[3].Text.ToString()));
            this.TextBox3.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[4].Text.ToString()));
            this.TextBox4.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[2].Text.ToString()));
            this.DropDownList2.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[5].Text.ToString())); 
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            LabelStatus.Text = "";
            TextBox1.Enabled = false; TextBox2.Enabled = false; TextBox3.Enabled = false;
            TextBox4.Enabled = false; DropDownList2.Enabled = false;
            if (TextBox1.Text == "" | TextBox2.Text == "" | TextBox3.Text == "" | TextBox4.Text == "" | DropDownList2.Text == "")
                LabelStatus.Text = "bạn chưa chọn nhân sự";
            else
            {
                String s = TextBox1.Text;
                DataRow row = dt.Select("MaNS = '" + s + "'")[0];
                row.BeginEdit();
                row.Delete();
                row.EndEdit();
                SqlCommand commandDelete = new SqlCommand();
                commandDelete.Connection = conn.con;
                commandDelete.CommandType = CommandType.Text;
                commandDelete.CommandText = @"Delete from NhanSu Where MaNS=@MaNS";
                commandDelete.Parameters.Add("@MaNS", SqlDbType.VarChar, 10, "MaNS");
                da.DeleteCommand = commandDelete;
                da.Update(dt);
                dt.Clear();
                getdata();
                LabelStatus.Text = "bạn đã xóa thành công";
                LabelMaNS.Text = "";
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            LabelStatus.Text = "";
            if (TextBox1.Text == "" | TextBox2.Text == "" | TextBox3.Text == "" | TextBox4.Text == "" | DropDownList2.Text == "")
                LabelStatus.Text = "bạn chưa chọn nhân sự";
            else
            {
                String s = TextBox1.Text;
                DataRow row = dt.Select("MaNS = '" + s + "'")[0];
                row.BeginEdit();
                row["MaNS"] = TextBox1.Text;
                row["LoaiNhanSu"] = TextBox4.Text;
                row["Gia"] = TextBox2.Text;
                row["MoTa"] = TextBox3.Text;
                row["TrangThai"] = DropDownList2.Text;
                row.EndEdit();
                SqlCommand commandUpdate = new SqlCommand();
                commandUpdate.Connection = conn.con;
                commandUpdate.CommandType = CommandType.Text;
                commandUpdate.CommandText = @"Update NhanSu Set LoaiNhanSu= @LoaiNhanSu,
                                                              Gia= @Gia,
                                                              Mota= @Mota,
                                                              TrangThai= @TrangThai
                                                              where MaNS= @MaNS";
                commandUpdate.Parameters.Add("@MaNS", SqlDbType.VarChar, 10, "MaNS");
                commandUpdate.Parameters.Add("@LoaiNhanSu", SqlDbType.NVarChar, 50, "LoaiNhanSu");
                commandUpdate.Parameters.Add("@Gia", SqlDbType.Money, 50, "Gia");
                commandUpdate.Parameters.Add("@Mota", SqlDbType.NVarChar, 50, "Mota");
                commandUpdate.Parameters.Add("@TrangThai", SqlDbType.Int, 10, "TrangThai");
                da.UpdateCommand = commandUpdate;
                da.Update(dt);
                dt.Clear();
                getdata();
                LabelStatus.Text = "bạn đã chỉnh sửa thành công";
            }
        }

        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            int dong = GridView1.SelectedIndex;
            this.TextBox1.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[0].Text.ToString()));
            this.TextBox2.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[2].Text.ToString()));
            this.TextBox3.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[3].Text.ToString()));
            this.TextBox4.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[1].Text.ToString()));
            this.DropDownList2.Text = HttpUtility.HtmlDecode((string)(GridView1.Rows[dong].Cells[4].Text.ToString())); 
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            TextBox1.Enabled = true;
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            LabelStatus.Text = "";
            LabelMaNS.Text = "";
        }
    }
}